<?php

/*

***************************************
*                                     *
* Copyright 2011 David Matthew Pugh   *
* Contact Info:thebravedave@gmail.com *
*                                     *
***************************************

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/     

function USPSParcelRate($dest_zip, $service_id, $pounds, $ounces) 
{    
      
    $configarray = file('./configurational/usps.php');
    
    $usernamestring = "";
    $originatingzipcodestring = "";
    $uspsServiceURLstring = "";
    foreach($configarray as $key => $value)
    {
        if(strpos($value, "originatingzipcode") !== false)
        {
           $originatingzipcodestring = $value; 
        }
        if(strpos($value, "username") !== false)
        {
           $usernamestring = $value; 
        }
        if(strpos($value, "uspsServiceURL") !== false)
        {
           $uspsServiceURLstring = $value; 
        }
    }
    
    $usernameArray = explode("=", $usernamestring);
    $origzipArray = explode("=", $originatingzipcodestring);
    $serviceurlArray = explode("=", $uspsServiceURLstring);
    $userName = trim($usernameArray[1]); 
    $orig_zip = trim($origzipArray[1]);
    $url = trim($serviceurlArray[1]); 
    
    $ch = curl_init();

    // set the target url
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

    // parameters to post
    curl_setopt($ch, CURLOPT_POST, 1);

    $data = "API=RateV3&XML=<RateV3Request USERID=\"$userName\"><Package ID=\"1ST\"><Service>$service_id</Service><FirstClassMailType>PARCEL</FirstClassMailType><ZipOrigination>$orig_zip</ZipOrigination><ZipDestination>$dest_zip</ZipDestination><Pounds>$pounds</Pounds><Ounces>$ounces</Ounces><Size>REGULAR</Size><Machinable>TRUE</Machinable></Package></RateV3Request>";
 
    // send the POST values to USPS
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

    $result=curl_exec($ch);
    
    $result = urldecode($result);

    $data = strstr($result, '</MailService>');
    $data = "<xml><RateV3Response><Package><Postage><MailService>" . $data . '</xml>';
 
    $xml = new SimpleXMLElement($data);
    $return = $xml->RateV3Response->Package->Postage->Rate;

    return $return;
    // echo '<!-- '. $data. ' -->'; // Uncomment to show XML in comments
  
   
}



?>
